package com.vayne.injectdemo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 接口回调注入 {@link org.springframework.beans.factory.Aware}
 *
 * @author xuerui.zhang
 * @since 2020/4/13 下午9:22
 */
public class AwareAnnotationInjectionDemo implements BeanFactoryAware, ApplicationContextAware {

    private BeanFactory beanFactory;
    private ApplicationContext applicationContext;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(AwareAnnotationInjectionDemo.class);
        context.refresh();
        AwareAnnotationInjectionDemo demo = context.getBean(AwareAnnotationInjectionDemo.class);
        System.out.println(demo.beanFactory == context.getBeanFactory());
        System.out.println(demo.applicationContext == context);
        System.out.println();
        context.close();
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
